//
//  ViewController.swift
//  circleMartix
//
//  Created by David on 2017/3/6.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var grid: YUGirdView!
    
    var duration = 0.1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        grid = YUGirdView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.width), x: 10, y: 10)
        grid.center.y = view.center.y
        view.addSubview(grid)

        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        exec()
    }
    
    func exec(){
        let n = grid.NumOfX
        var s = 0
        for i in 0..<n{
            
            if i < n-i{
                for j in i..<n-i{
                    UIView.animate(withDuration: self.duration, delay: Double(s)*self.duration, options: .curveLinear, animations: {
                        self.grid.cell[i][j].backgroundColor = UIColor.yellow
                    }, completion: { (finished) in
                        UIView.animate(withDuration: self.duration, delay: Double(s)*self.duration, options: .curveLinear, animations: {
                            self.grid.cell[i][j].backgroundColor = UIColor.gray
                        }, completion: nil)
                    })
                    s += 1
                }
            }
            
            
            
            let c = n - i - 1
            if i+1 < c+1 {
                for k in i+1..<c+1{
                    UIView.animate(withDuration: self.duration, delay: Double(s)*self.duration, options: .curveLinear, animations: {
                        self.grid.cell[k][c].backgroundColor = UIColor.yellow
                    }, completion: { (finished) in
                        UIView.animate(withDuration: self.duration, delay: Double(s)*self.duration, options: .curveLinear, animations: {
                            self.grid.cell[k][c].backgroundColor = UIColor.gray
                        }, completion: nil)
                    })
                    s += 1
                }
            }
            
            if i < c{
                for l in i..<c{
                    let ll = n - 2 - l
                    UIView.animate(withDuration: self.duration, delay: Double(s)*self.duration, options: .curveLinear, animations: {
                        self.grid.cell[c][ll].backgroundColor = UIColor.yellow
                    }, completion: { (finished) in
                        UIView.animate(withDuration: self.duration, delay: Double(s)*self.duration, options: .curveLinear, animations: {
                            self.grid.cell[c][ll].backgroundColor = UIColor.gray
                        }, completion: nil)
                    })
                    s += 1
                }
            }
            
            if i < c-1 {
                for m in i..<c-1{
                    let mm = n - 2 - m
                    UIView.animate(withDuration: self.duration, delay: Double(s)*self.duration, options: .curveLinear, animations: {
                        self.grid.cell[mm][i].backgroundColor = UIColor.yellow
                    }, completion: { (finished) in
                        UIView.animate(withDuration: self.duration, delay: Double(s)*self.duration, options: .curveLinear, animations: {
                            self.grid.cell[mm][i].backgroundColor = UIColor.gray
                        }, completion: nil)
                    })
                    s += 1
                }
            }
            
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

